package com.beasts.antpage.sh.controller;

import com.beasts.antpage.core.common.token.AntUCToken;
import com.beasts.antpage.core.utils.json.JacksonUtil;
import com.beasts.antpage.sh.entity.SysUserManage;
import com.beasts.antpage.sh.entity.User;
import com.beasts.antpage.sh.service.IAuthLoginService;
import com.beasts.antpage.sh.service.IUserInfoService;
import com.crux.cloud.app.common.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 入口
 *
 * @author Locker
 * @date 07/05/2018 8:39 AM
 * @since 1.0
 */
@Controller
public class LoginController {

    @Autowired
    private IAuthLoginService iAuthLoginService;

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 登录页
     *
     * @return
     */
    @RequestMapping({"/", "/homePage"})
    public String login() {
        return "homePage";
    }

    /**
     * 登录验证
     *
     * @return
     */
    @RequestMapping({"/loginCheck"})
    @ResponseBody
    public Map<String, Object> authLogin(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Map<String, Object> map = new HashMap<>();
        try {

            String fullToken = AntUCToken.getToken(username, password);
            String token = (String) JacksonUtil.getProperty(fullToken, "access_token");
            map.put("token", token);
            map.put("success", true);
            map.put("message", "登录成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "登录失败，用户名或密码错误！");
            return map;
        }

        return map;
    }

    /**
     * 查询用户by userId
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public User searchUserInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        String userId = req.getParameter("userId");
        User user = new User();
        try {
            user = iAuthLoginService.getUser(userId);
            return user;
        } catch (Exception e) {
            return user;
        }
    }

    /**
     * 查询用户by token
     */
    @RequestMapping("/getUserByToken")
    @ResponseBody
    public Map<String, Object> findUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String tokenId = req.getParameter("tokenId");
        Map<String, Object> userMap = new HashMap<>();
        try {
            userMap = AntUCToken.getUserByToken(tokenId);
            return userMap;
        } catch (Exception e) {
            return userMap;
        }
    }

    /**
     * 调用平台接口获取用户
     */
    @RequestMapping("/getUserInfoFromPF")
    @ResponseBody
    public UserInfo getCurrentUserInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String> paramMap = new HashMap<>();
        try {

            UserInfo userInfo = userInfoService.getCurrentUser();
            return userInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    /**
     * 查询用户by token
     */
    @RequestMapping("/findByUserId")
    @ResponseBody
    public SysUserManage getUserById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            SysUserManage sysUserManage = new SysUserManage();

            String id = req.getParameter("userId");
            sysUserManage = iAuthLoginService.findByUserId(id);
            return sysUserManage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询用户by token
     */
    @RequestMapping("/saveUsermanage")
    @ResponseBody
    public SysUserManage saveUsermanage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            SysUserManage sysUserManage = new SysUserManage();
            String userId = req.getParameter("userId");
            sysUserManage.setUserId(userId);
            sysUserManage.setUserCode(userId);
            sysUserManage.setOrgType("1");
            sysUserManage.setLevelNum("2");
            sysUserManage.setEnabled("1");
            sysUserManage.setUser_Type("1");
            sysUserManage.setIsAudit("1");
            sysUserManage.setSetYear("2018");
            sysUserManage.setIscombinerole("1");


            sysUserManage = iAuthLoginService.saveUsermanage(sysUserManage);
            return sysUserManage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
